package cn.zime.iot.view;

import cn.zime.iot.entity.User;
import cn.zime.iot.entity.UserDao;
import cn.zime.iot.util.CaptchaUtil;
import cn.zime.iot.util.DBUtil;
import cn.zime.iot.util.StringUtil;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.util.Objects;

public class RegisterAccountView extends JFrame {
	private JPanel contentPane;
	private JTextField userNameTxt;
	private JPasswordField passwordTxt;
	private JPasswordField re_passwordTxt;
	private JTextField capTxt;
	private JTextField emailTxt;
	private JLabel captcha;
	private UserDao userDao = new UserDao();
	private DBUtil dbUtil = new DBUtil();
	private String capStr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			try {
				RegisterAccountView frame = new RegisterAccountView();
				frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegisterAccountView() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 500, 403);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("注册账号");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setBounds(208, 10, 96, 34);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("账号");
		lblNewLabel_1.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/账号.png"))));
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_1.setBounds(107, 70, 65, 15);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("新密码");
		lblNewLabel_2.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/新密码.png"))));
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_2.setBounds(107, 110, 76, 15);
		contentPane.add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("确认密码");
		lblNewLabel_3.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/确认密码.png"))));
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_3.setBounds(107, 160, 87, 15);
		contentPane.add(lblNewLabel_3);

		JLabel email = new JLabel("Email");
		email.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/账号.png"))));
		email.setFont(new Font("宋体", Font.PLAIN, 13));
		email.setBounds(107, 205, 65, 15);
		contentPane.add(email);

		JLabel lblNewLabel_4 = new JLabel("验证码");
		lblNewLabel_4.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/验证码.png"))));
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 13));
		lblNewLabel_4.setBounds(107, 252, 76, 15);
		contentPane.add(lblNewLabel_4);

		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(this::regActionPerformed);
		btnNewButton.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/确认.png"))));
		btnNewButton.setBounds(124, 303, 93, 23);
		contentPane.add(btnNewButton);

		userNameTxt = new JTextField();
		userNameTxt.setBounds(204, 70, 170, 21);
		contentPane.add(userNameTxt);
		userNameTxt.setColumns(10);

		passwordTxt = new JPasswordField();
		passwordTxt.setBounds(204, 110, 170, 21);
		contentPane.add(passwordTxt);
		passwordTxt.setColumns(10);

		re_passwordTxt = new JPasswordField();
		re_passwordTxt.setBounds(204, 160, 170, 21);
		contentPane.add(re_passwordTxt);
		re_passwordTxt.setColumns(10);

		capTxt = new JTextField();
		capTxt.setBounds(204, 250, 85, 21);
		contentPane.add(capTxt);
		capTxt.setColumns(10);

		capStr = CaptchaUtil.getCapStr();
		captcha = new JLabel(capStr);
		captcha.setBounds(300, 250, 85, 21);
		contentPane.add(captcha);

		emailTxt = new JTextField();
		emailTxt.setBounds(204, 205, 170, 21);
		contentPane.add(emailTxt);
		emailTxt.setColumns(10);


		JButton cancelBtn = new JButton("取消");
		cancelBtn.setIcon(new ImageIcon(Objects.requireNonNull(FindPassword.class.getResource("/image/取消.png"))));
		cancelBtn.setBounds(282, 303, 93, 23);
		contentPane.add(cancelBtn);
		cancelBtn.addActionListener(e -> {
			this.dispose();
		});
		setIconImage(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/1.png"))).getImage());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
		setLocationRelativeTo(null);
	}
	private void regActionPerformed(ActionEvent e) {
		String userName=this.userNameTxt.getText();
		String passwordTxt=new String(this.passwordTxt.getPassword());
		String rePasswordTxt=new String(this.re_passwordTxt.getPassword());
		if (!this.capTxt.getText().equals(capStr.toLowerCase())) {
			JOptionPane.showMessageDialog(null, "验证码错误！");
			return;
		}
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}if(StringUtil.isEmpty(passwordTxt)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}if(StringUtil.isEmpty(rePasswordTxt)) {
			JOptionPane.showMessageDialog(null, "确认密码不能为空！");
			return;
		}if(passwordTxt.equals(rePasswordTxt)) {
			Connection con=null;
			User user=new User(userName,passwordTxt);
			try {
				con=dbUtil.getConn();
				int addNum=userDao.addUser(con, user);
				if(addNum==1) {
					JOptionPane.showMessageDialog(null, "注册成功！");
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "注册失败！");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				dbUtil.closeConn(con);
			}
		}else {
			JOptionPane.showMessageDialog(null, "两次密码不一致！");
			return;
		}

	}
}
